package com.springapp.controller;

/**
 * Created by nicol_000 on 14/03/14.
 */



import com.springapp.dao.dao.impl.MouvementDAO;
import com.springapp.model.Mouvement;
import com.springapp.service.impl.FournisseurService;
import com.springapp.service.impl.IncidentService;
import com.springapp.service.impl.ListeTachesService;
import com.springapp.service.impl.MouvementService;
import com.sun.swing.internal.plaf.synth.resources.synth_sv;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.text.SimpleDateFormat;
import java.util.Date;


@Controller
@RequestMapping(value="/listeTaches",method=RequestMethod.GET)
public class ControlLstTaches {

    @Autowired
    private ListeTachesService sceTache;

    @Autowired
    private IncidentService incSce;

    @Autowired
    private FournisseurService fourSce;

    @Autowired
    private MouvementDAO mouvDAO;

    @RequestMapping(value="/attribTaches", method = RequestMethod.GET)
    public ModelAndView displayTaches() {
        ModelAndView model = new ModelAndView("listeTachesCarriste");
        model.addObject("laTache", sceTache.getTacheAuto());
        model.addObject("totalTaches",sceTache.getLstTachesglobal().size());
        model.addObject("lstIncident",incSce.getAll());
        model.addObject("lstFour",fourSce.getAll());
        return model;
    }

    @RequestMapping(value="/ajxscanpal",method=RequestMethod.POST)
    public @ResponseBody
    String verifTacheOK(@RequestParam(value="ajxScanBarre") int ajxScanBarre,
                          @RequestParam(value="ajxIdArt") int ajxIdArt,
                          @RequestParam(value="ajxZoneDepart") int ajxZoneDepart,
                          @RequestParam(value="ajxNbreArticle") int ajxNbreArticle){
        String ret= sceTache.PaletteOK(ajxScanBarre,ajxIdArt, ajxNbreArticle,ajxZoneDepart);
        return ret;
    }

   @RequestMapping(value="/ajxscanzone",method=RequestMethod.POST)
    public @ResponseBody
    String renvoiLibZone(@RequestParam(value="ajxZone") String ajxZone,
                         @RequestParam(value="ajxZoneDepart") int ajxZoneDepart){
        String rep=sceTache.verifEmplacement(Integer.parseInt(ajxZone), ajxZoneDepart);

        return rep;
    }

    @RequestMapping(value="/depotpalettezone",method=RequestMethod.POST)
    public @ResponseBody
    String deposePalette(@RequestParam(value="ajxZoneDepart") String ajxZoneDepart,
                         @RequestParam(value="idPalette") int idPalette,
                         @RequestParam(value="ajxZoneArrivee") int idZoneArr,
                         @RequestParam(value="ajxZoneDemandee") int idZoneDemandee,
                         @RequestParam(value="idTache") int idTache){
        System.out.println("ID Tache : "+idTache);
        String rep=sceTache.achemineInterne(Integer.parseInt(ajxZoneDepart),idZoneDemandee,idPalette,idZoneArr);
        if(rep.equals("Mouvement enregistré")){
            sceTache.supprimeTache(idTache);
        }
        return rep;
    }

    @RequestMapping(value="/declareincident",method=RequestMethod.POST)
    public @ResponseBody
    String enregMouvIncident(@RequestParam(value="ajxTypeMouv") int ajxTypeMouv,
                         @RequestParam(value="ajxIdPal") int ajxIdPal,
                         @RequestParam(value="ajxqteArt") int ajxqteArt,
    //                     @RequestParam(value="ajxidFour") int ajxidFour,
                         @RequestParam(value="ajxIdIncident") int ajxIdIncident,
                         @RequestParam(value="ajxIdArticleFour") int ajxIdArticleFour,
                         @RequestParam(value="ajxZoneDepart") int idzonedepart){
        String rep="";
        Mouvement mouv=new Mouvement(0,"sortie",ajxTypeMouv,ajxIdIncident,"","",ajxIdPal,ajxIdArticleFour,ajxqteArt,"");
        rep=sceTache.enregIncident(mouv,idzonedepart);
        return rep;
    }
}
